package com.wck.car.oyz;

import com.wck.car.model.CarBrandDesc;
import com.wck.car.model.CarBrandInfo;
import com.wck.car.model.CarSeriesDesc;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 获得车辆系列详情
 */
public class CarSeriesDescType {

    public static List<CarSeriesDesc> getCarSeriesHtml(CarBrandDesc carBrandDesc){
        String carSeriesBody=SpiderCar.getBody("https://m.evpartner.com/"+carBrandDesc.getCarLink());
        Document document = Jsoup.parse(carSeriesBody);
        Elements carInfo = document.getElementsByClass("carInfo");
        Element element = carInfo.get(0);
        Elements dl = element.getElementsByTag("dl");
        carBrandDesc.setCarSize(dl.get(1).getElementsByClass("black").get(0).text());
        carBrandDesc.setCarLoad(dl.get(2).getElementsByClass("black").get(0).text());
        carBrandDesc.setCarElectricity(dl.get(3).getElementsByClass("black").get(0).text());

        Elements carSeries = document.getElementsByClass("dnsjn");
        Elements ul = carSeries.get(0).getElementsByTag("ul");
        ArrayList<CarSeriesDesc> carSeriesDescArrayList = new ArrayList<>();
        ul.forEach(s ->{
            CarSeriesDesc carSeriesDesc=new CarSeriesDesc();
            carSeriesDesc.setCarSeriesName(s.getElementsByClass("carNamecarName").text());   //找打车辆系列名称
            carSeriesDesc.setCarSeriesImg(s.getElementsByClass("carNamecarName-img").attr("src"));   //找到车辆系列照片
            carSeriesDesc.setCarSeriesPrice(s.getElementsByClass("price").text());    //找到车辆系列厂商指导价
            carSeriesDesc.setCarSeriesLink(s.getElementsByClass("carName").get(0).select("a[href]").attr("href"));
            String id=s.getElementsByClass("carName").get(0).select("a[href]").attr("href").split("-")[1].split("\\.")[0];
            carSeriesDesc.setId(Long.parseLong(id));
            carSeriesDesc.setCarBrandId(carBrandDesc.getId());
            carSeriesDescArrayList.add(carSeriesDesc);
        });
        return carSeriesDescArrayList;
    }

}
